import {
    anit,
    shake
} from '../../utils/util.js'
import {
    addBindCompanySupplier,
    getBindCompanySupplier,
    updateCompanySupplier,
    getEnterpriseList,
    getquerycompanyconfigapi,
} from '../../api/app.js'
import user from '../../utils/user.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        enterprise: {
            name: '',
            director: '',
            phone: '',
            creditCode: '',
            ofSystem: false,
            isNeedIdcardPhoto: true,
            isShowContacts: false,
            isOpenAdvance: false
        },
        tipList: [],
        openadvance: true,
        popupflag: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    opensearchqy() {
        this.setData({
            ['enterprise.name']: '',
            tipList: []
        })
        this.selectComponent('#bottomFrame').showFrame()
    },
    onLoad(options) {
        getquerycompanyconfigapi().then((res) => {
            console.log(res)
            if (res.id) {
                this.setData({
                    openadvance: true
                })
            } else {
                this.setData({
                    openadvance: false
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    // 提交企业
    onSubmitEnterprise() {
        const data = this.data.enterprise
        console.log(data, 'datadata')
        if (data.phone.length > 0 && !/^1\d{10}$/.test(data.phone)) {
            app.toast('请输入正确的手机号码')
            return 0
        }
        // 企业名称不来源于后台提供
        if (!data.ofSystem) {
            app.toast('请在下拉列表中选择对应的企业名称')
            return 0
        }
        if (!data.name) {
            app.toast('请选择企业')
            return 0
        }
        // this.setData({
        //     enterprise: {}
        // })
        addBindCompanySupplier({
            companyId: user.companyId,
            serveCompanyName: data.name,
            creditCode: data.creditCode,
            contacts: data.director,
            area: data.base || '暂无',
            phone: data.phone,
            createUserId: user.userId,
            isNeedIdcardPhoto: Number(data.isNeedIdcardPhoto),
            contactsId: data.id,
            isOpenAdvance: Number(data.isOpenAdvance)
        }).then(res => {
            console.log(res, '添加啦')
            app.toast('添加成功', 'success')
            const pages = getCurrentPages()
            // 获取上一个页面实例
            const page = pages[pages.length - 2]
            // 调用上一个页面的实例，将选择的员工传回
            page['addsuccess'](res)
            // 回到上一个界面
            wx.navigateBack()
        })
    },
    back() {
        wx.navigateBack()
    },
    toprivel() {
        if (this.data.openadvance) {
            wx.navigateTo({
                url: '/pages/previewAdvancegz/previewAdvancegz',
            })
        } else {
            return
        }
    },
    copy() {
        wx.setClipboardData({
            data: 'https://web.51zidan.com',
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data)
                    }
                })
            }
        })
    },
    onChangeNeedCardPhoto(event) {
        this.setData({
            ['enterprise.isNeedIdcardPhoto']: event.detail.value
        })
    },
    closepopup1() {
        this.setData({
            popupflag: false,
            ['enterprise.isShowContacts']: true
        })
    },
    closepopup2() {
        this.setData({
            popupflag: false,
            ['enterprise.isShowContacts']: false
        })
    },
    onChangeisShowContacts(event) {
        if (!event.detail.value) {
            this.setData({
                popupflag: true
            })
        } else {
            this.setData({
                popupflag: false
            })
        }
        this.setData({
            ['enterprise.isShowContacts']: event.detail.value
        })
    },
    onChangeisOpenAdvance(event) {
        this.setData({
            ['enterprise.isOpenAdvance']: event.detail.value
        })
    },
    selectDirectorComplete(data) {
        console.log(data)
        this.setData({
            ['enterprise.director']: data[0].name,
            ['enterprise.phone']: data[0].phone,
            ['enterprise.id']: data[0].id
        })
    },
    onTapEnter(event) {
        this.setData({
            ['enterprise.ofSystem']: true,
            ['enterprise.name']: event.currentTarget.dataset.name,
            ['enterprise.creditCode']: event.currentTarget.dataset.code,
            ['enterprise.base']: event.currentTarget.dataset.base,
            tipList: []
        })
        this.selectComponent('#bottomFrame').hideFrame()
    },
    onEnterpriseName(event) {
        if (event.detail.value) {
            shake(() => {
                this.setData({
                    ['enterprise.creditCode']: ''
                })

                getEnterpriseList({
                    name: event.detail.value
                }).then(res => {
                    this.setData({
                        tipList: res
                    })
                })
            })
        }
    },
    onSelectDirector() {
        if (anit)
            wx.navigateTo({
                url: '/pages/team-list/team-list?select=selectDirectorComplete',
            })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})